######################################################################
package Swignition::GenericParser::Recursive;
######################################################################

use Swignition::Misc;
use Swignition::Export;
use Swignition::GenericParser::RDF;
use Swignition::GenericParser::Utils;
use Clone qw(clone);
use strict;

sub is_enabled
{
	my $this = shift;
	return ($this->{options}->{p_rdfx} > 0);
}

sub consume
{
	my $this = shift;
	my @links =  allOfObj($this->{RDF}->{ $this->uri }, 'http://www.w3.org/1999/xhtml/vocab#meta');
	push @links, allOfObj($this->{RDF}->{ $this->uri }, 'http://www.w3.org/2000/01/rdf-schema#seeAlso');
	push @links, allOfObj($this->{RDF}->{ $this->uri }, 'http://www.iana.org/assignments/relation/meta');

	foreach my $link ( @links )
	{
		my $uri = URI->new( $link->toString );
		if ($this->{NoFollow}->{ $uri->host })
		{
			$this->{'RDF'}->{"$uri"} = Swignition::RDFModel::Subject->new("$uri")
				unless $this->{'RDF'}->{"$uri"};
			$this->{'RDF'}->{"$uri"}->addVal('http://www.w3.org/2000/01/rdf-schema#comment',
				Swignition::RDFModel::Value->new("Was not followed because of NoFollow rule for host ".$uri->host."."));

			next;
		}
		
		my $request  = HTTP::Request->new(GET => "$uri");
		my $response = Swignition::Misc::get_ua->request($request);

		# Set options for a recursive parse.
		my $opts          = clone($this->{options});
		$opts->{ua}       = $this->get_ua;
		$opts->{request}  = $request;
		$opts->{response} = $response;
		$opts->{base}     = "$uri";
		$opts->{p_rdfx}--;
		
		# Recursive parse.
		my $external_page = Swignition::GenericParser::new_by_type($response->content, $opts);
		
		# If MIME type was unrecognised by new_by_type, then try as RDF/XML.
		$external_page = Swignition::RdfXmlParser->new($response->content, $opts)
			if (!$external_page);
		
		# If we've actually managed to parse something...
		if ($external_page)
		{
			# Export what we got as Turtle
			my $turtle = Swignition::Export::Entity::multiTurtle( $external_page->{RDF} );
			# Then merge the Turtle into our graph.
			Swignition::GenericParser::RDF::handle($this, $turtle, "$uri", 'turtle');
		}
	}
} #/sub consume

1;
